package com.example.furniture.controller;

import com.example.furniture.service.StatisticsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 统计信息控制器
 * 提供系统统计数据的查询功能
 * 
 * @author 家具商城系统
 * @version 1.0
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/statistics")
@CrossOrigin(origins = "*") // 允许跨域访问，用于前端调用
public class StatisticsController {

    private static final Logger logger = LoggerFactory.getLogger(StatisticsController.class);
    private final StatisticsService statisticsService;

    /**
     * 构造函数，注入统计服务
     * 
     * @param statisticsService 统计服务实例
     */
    public StatisticsController(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    /**
     * 获取系统统计概览
     * 包括用户总数、家具总数、分类总数、库存总价值等
     * 
     * @return 统计概览数据
     */
    @GetMapping("/overview")
    public ResponseEntity<Map<String, Object>> getOverview() {
        try {
            logger.info("获取系统统计概览");
            Map<String, Object> overview = statisticsService.getOverview();
            logger.info("成功获取统计概览数据");
            return ResponseEntity.ok(overview);
        } catch (Exception e) {
            logger.error("获取统计概览失败", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取家具统计信息
     * 包括按分类统计的家具数量、价格分布等
     * 
     * @return 家具统计数据
     */
    @GetMapping("/furniture")
    public ResponseEntity<Map<String, Object>> getFurnitureStatistics() {
        try {
            logger.info("获取家具统计信息");
            Map<String, Object> statistics = statisticsService.getFurnitureStatistics();
            logger.info("成功获取家具统计信息");
            return ResponseEntity.ok(statistics);
        } catch (Exception e) {
            logger.error("获取家具统计信息失败", e);
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取库存统计信息
     * 包括库存不足的家具、库存总价值等
     * 
     * @return 库存统计数据
     */
    @GetMapping("/inventory")
    public ResponseEntity<Map<String, Object>> getInventoryStatistics() {
        try {
            logger.info("获取库存统计信息");
            Map<String, Object> statistics = statisticsService.getInventoryStatistics();
            logger.info("成功获取库存统计信息");
            return ResponseEntity.ok(statistics);
        } catch (Exception e) {
            logger.error("获取库存统计信息失败", e);
            return ResponseEntity.status(500).build();
        }
    }
}
